//
//  IndexMenuCtrl.m
//  cutter
//
//  Created by zhouliangfei on 16/6/1.
//  Copyright © 2016年 e360. All rights reserved.
//
#import "IndexMenuCtrl.h"
#import "UserCenterPick.h"
@interface IndexMenuButton : UIButton
@end
@implementation IndexMenuButton
-(void)layoutSubviews{
    [super layoutSubviews];
    [self setTitleEdgeInsets:UIEdgeInsetsMake(0, 0-self.imageView.bounds.size.width, 0, 4+self.imageView.bounds.size.width)];
    [self setImageEdgeInsets:UIEdgeInsetsMake(0, 4+self.titleLabel.bounds.size.width, 0, 0-self.titleLabel.bounds.size.width)];
}
@end
//
@interface IndexMenuCtrl ()
@property (weak, nonatomic) IBOutlet UILabel *titleView;
@property (weak, nonatomic) IBOutlet UITextField *searchView;
@property (weak, nonatomic) IBOutlet UIButton *commitView;
@property (weak, nonatomic) IBOutlet UIButton *clientView;
@property (weak, nonatomic) IBOutlet UIButton *showView;
@property (weak, nonatomic) IBOutlet UIButton *listView;
@end
@implementation IndexMenuCtrl
-(void)viewDidLoad {
    [super viewDidLoad];
    [self.searchView setAlpha:0];
    [self.searchView setLeftView:self.listView];
    [self.searchView setLeftViewMode:UITextFieldViewModeAlways];
    //
    [self.commitView setSelected:YES];
    [self commitTouch:self.commitView];
    [self.commitView addTarget:self action:@selector(commitTouch:) forControlEvents:UIControlEventTouchUpInside];
}
-(BOOL)shouldPerformSegueWithIdentifier:(NSString *)identifier sender:(id)sender{
    if([TMGlobal valueForKey:@"client"]) {
        [TMGlobal setValue:nil forKey:@"clientTag"];
        if ([identifier isEqualToString:@"cart"]) {
            [TMGlobal setValue:[NSNumber numberWithInt:4] forKey:@"clientTag"];
        }
        if ([identifier isEqualToString:@"favorite"]) {
            [TMGlobal setValue:[NSNumber numberWithInt:3] forKey:@"clientTag"];
        }
        [TMGlobal setValue:[NSNumber numberWithInt:8] forKey:@"tabBarTag"];
        return NO;
    }
    return YES;
}
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
    if ([segue.destinationViewController isKindOfClass:UINavigationController.class]) {
        UINavigationController *ctrl = segue.destinationViewController;
        if ([[ctrl.viewControllers firstObject] isKindOfClass:UserCenterSelect.class]) {
            __weak typeof(self) this = self;
            UserCenterSelect *root = [ctrl.viewControllers firstObject];
            [root setOnSelected:^(id target, id result){
                if(result){
                    [this shouldPerformSegueWithIdentifier:segue.identifier sender:sender];
                }
                [this dismissViewControllerAnimated:YES completion:nil];
            }];
        }
    }
    //
    UIPopoverPresentationController *popover = [segue.destinationViewController popoverPresentationController];
    if ([sender isKindOfClass:UIView.class] && [popover isKindOfClass:UIPopoverPresentationController.class]) {
        [popover setSourceRect:[(UIView*)sender bounds]];
        [popover setSourceView:(UIView*)sender];
    }
}
//
-(void)commitTouch:(UIButton*)sender{
    [sender setSelected:!sender.selected];
    //
    if (sender.selected) {
        [UIView beginAnimations:nil context:nil];
        [self.titleView setAlpha:0];
        [self.searchView setAlpha:1];
        [UIView commitAnimations];
        //
        [self.commitView setImage:nil forState:UIControlStateNormal];
        [self.commitView setTitle:@"取消" forState:UIControlStateNormal];
    }else{
        [UIView beginAnimations:nil context:nil];
        [self.titleView setAlpha:1];
        [self.searchView setAlpha:0];
        [UIView commitAnimations];
        //
        [self.commitView setTitle:nil forState:UIControlStateNormal];
        [self.commitView setImage:[UIImage imageNamed:@"btn_Top_Search_nor.png"] forState:UIControlStateNormal];
    }
}
-(void)setTitle:(NSString *)title{
    [super setTitle:title];
    if (title) {
        [self.titleView setText:title];
    }else{
        [self.titleView setText:@""];
    }
}
-(void)setClient:(id)client{
    if (client && [client objectForKey:@"truename"]) {
        [self.clientView setTitle:[client objectForKey:@"truename"] forState:UIControlStateNormal];
    }else{
        [self.clientView setTitle:@"" forState:UIControlStateNormal];
    }
}
@end
